


package yf.mall.controller;
import com.yf.request.SearchParam;

import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.MallGoodsService;
import yf.mall.service.UserService;
import yf.mall.vo.CommentVo;
import yf.mall.service.CommentService;
import com.yf.response.R;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
                                                import yf.mall.enums.CommentStatusEnum;
    
/**
 * 评论Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Sep 15 10:24:34 CST 2020
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private MallGoodsService mallGoodsService;

    /**
     * 列表评论
     */
    @SysLog("查看评论")
    @RequestMapping("/list")
    @RequiresPermissions("comment:list")
    public R list(@RequestBody SearchParam<CommentVo> params) {
        if(params.getEntity().getComment().getUserCode() != null){
            UserEntity userEntity = userService.findByCode(params.getEntity().getComment().getUserCode());
            if (userEntity == null){
                throw new RRException("该编号不存在");
            }
            params.getEntity().getComment().setUserId(userEntity.getId());
        }
        List<MallGoodsEntity> goods = mallGoodsService.findGoods();

        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<CommentVo> list = commentService.findVo(params.getEntity());

        return R.ok(new PageInfo(list)).putExtra("goods",goods);
    }

    /**
 * 下载订单
 */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("comment:list")
    public void down(@RequestBody SearchParam<CommentVo> params, HttpServletResponse response) {
        List<CommentVo> list = commentService.findVo(params.getEntity());
            commentService.downLoad(response, list);
    }


    /**
     * 查看评论
     */
    @SysLog("查看评论")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("comment:info")
    public R info(@PathVariable("id") Long id) {
        CommentVo vo = commentService.getVo(id);
        if (vo.getEntity().getUserId() != null){
            UserEntity userEntity = userService.get(vo.getEntity().getUserId());
            vo.getEntity().setUserCode(userEntity.getCode());
        }
        return R.ok(vo);
    }

    /**
     * 保存评论
     */
    @SysLog("保存评论")
    @RequestMapping("/save")
    @RequiresPermissions("comment:save")
    public R save(@RequestBody CommentVo vo) {
        if (vo.getEntity().getUserCode() != null){
            UserEntity userEntity = userService.findByCode(vo.getEntity().getUserCode());
            if (userEntity == null){
                throw  new RRException("该编号不存在");
            }
            vo.getEntity().setUserId(userEntity.getId());
        }
            commentService.saveVo(vo);
        return R.ok(vo);
    }

    /**
     * 修改评论
     */
    @SysLog("修改评论")
    @RequestMapping("/update")
    @RequiresPermissions("comment:update")
    public R update(@RequestBody CommentVo vo) {
        if (vo.getEntity().getUserCode() != null){
            UserEntity userEntity = userService.findByCode(vo.getEntity().getUserCode());
            if (userEntity == null){
                throw  new RRException("该编号不存在");
            }
            vo.getEntity().setUserId(userEntity.getId());
        }
            commentService.updateVo(vo);

        return R.ok(vo);
    }

    /**
     * 删除评论
     */
    @SysLog("删除评论")
    @RequestMapping("/delete")
    @RequiresPermissions("comment:delete")
    public R delete(Long[]ids) {
            commentService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有评论
     */
    @SysLog("所有评论")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order!=null ? order : "id desc");
        List<CommentVo> list = commentService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String,Object> options = new HashMap();
                                                                                                                                                                                                                        options.put("statusOptions",CommentStatusEnum.names());
                            return R.ok(options);
    }





    /***********自定义开始***********/
}
